package com.zjk.storage.core.provider.local;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.zjk.storage.core.entity.BaseFileInfo;
import com.zjk.storage.core.entity.UploadPretreatment;
import com.zjk.storage.core.exception.FileStorageException;
import com.zjk.storage.core.provider.FileStorage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Consumer;


@Slf4j
@AllArgsConstructor
public class LocalFileStorage implements FileStorage {

    private final LocalConfig config;

    @Override
    public String getPlatform() {
        return config.getPlatform();
    }

    @Override
    public boolean upload(BaseFileInfo fileInfo, UploadPretreatment pre) {
        String basePath =StrUtil.addSuffixIfNot(config.getBasePath(), File.separator);
        String domain = StrUtil.addSuffixIfNot(config.getDomain(), File.separator);
        fileInfo.setBasePath(basePath);
        fileInfo.setUrl(domain + fileInfo.getPath() + fileInfo.getFileName());
        File newFile = FileUtil.touch(basePath + fileInfo.getPath() + fileInfo.getFileName());
        try {
            pre.getFileWrapper().transferTo(newFile);
            byte[] thumbnailBytes = pre.getThumbnailBytes();
            if (thumbnailBytes != null) {
                String thPath = basePath + fileInfo.getPath() + fileInfo.getThumbFilename();
                FileUtil.writeBytes(thumbnailBytes, thPath);
            }
            return true;
        } catch (Exception e) {
            FileUtil.del(newFile);
            log.error("platform:{},上传文件失败！ fileName:{},",config.getPlatform(),fileInfo.getOriginalName(),e);
            throw new FileStorageException("上传文件失败！",e);
        }
    }

    @Override
    public boolean delete(BaseFileInfo fileInfo) {
        if (fileInfo.getThumbFilename() != null) {
            FileUtil.del(new File(fileInfo.getBasePath() + fileInfo.getPath() + fileInfo.getThumbFilename()));
        }
        return FileUtil.del(new File(fileInfo.getBasePath() + fileInfo.getPath() + fileInfo.getFileName()));
    }

    @Override
    public boolean exists(BaseFileInfo fileInfo) {
        return new File(fileInfo.getBasePath() + fileInfo.getPath() + fileInfo.getFileName()).exists();
    }

    @Override
    public void download(BaseFileInfo fileInfo, Consumer<InputStream> consumer) {
        try (InputStream in = FileUtil.getInputStream(fileInfo.getBasePath() + fileInfo.getPath() + fileInfo.getFileName())) {
            consumer.accept(in);
        } catch (IOException e) {
            throw new FileStorageException("文件下载失败！platform：" + fileInfo, e);
        }
    }

    @Override
    public void downloadTh(BaseFileInfo fileInfo, Consumer<InputStream> consumer) {
        try (InputStream in = FileUtil.getInputStream(fileInfo.getBasePath() + fileInfo.getPath() + fileInfo.getThumbFilename())) {
            consumer.accept(in);
        } catch (IOException e) {
            throw new FileStorageException("缩略图文件下载失败！platform：" + fileInfo, e);
        }
    }

}
